package com.cq.health.utils;

import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.util.Auth;

/**
 * @Author: 悠久的苍穹
 * @Date: 2020/6/1 14:39
 * @Description: 七牛云工具类
 * @Version: 1.0
 */
public class QiniuUtils {
    public  static String accessKey = "eodhLVQz7zERSGK975MIJKv55iuXZ2I3lcdksRWa";
    public  static String secretKey = "WbqDPAaX9zxOLtDAbbH9oY2eqC2X2AyYcCblOXkt";
    public  static String bucket = "health-project-cq";
    /**
     * 删除文件
     * @param fileName 服务端文件名
     */
    public static void deleteFileFromQiniu(String fileName){
        //构造一个带指定Zone对象的配置类
        Configuration cfg = new Configuration(Zone.zone2());
        String key = fileName;
        Auth auth = Auth.create(accessKey, secretKey);
        BucketManager bucketManager = new BucketManager(auth, cfg);
        try {
            bucketManager.delete(bucket, key);
        } catch (QiniuException ex) {
            //如果遇到异常，说明删除失败
            System.err.println(ex.code());
            System.err.println(ex.response.toString());
        }
    }
    // 测试上传与删除
    public static void main(String args[]) throws Exception{
        // 测试删除
        deleteFileFromQiniu("2bca53d7a04e4a3db3389332b58c278b_man.jpg");
    }
}
